/* RMI-C Client : Echo Sample (v1) */

#include "rmi.h"
#include "echo.h"

/* ************************ CLIENT ************************ */

int main(int argc, char* argv[])
{
  if( argc < 5){
    fprintf(stderr,"Usage: ./client <host> <port> <naming-server host> <naming-server port>\n");
    exit(1);
  }
  char * hostname = argv[1];
  int port = atoi(argv[2]);
  char* naming_server_host = argv[3];
  int naming_server_port = atoi(argv[4]); 
  unsigned long object_adress = rmi_client_lookup("echo", naming_server_port, naming_server_host);
    
  CLIENT * client = rmi_client_create(hostname, port, object_adress, 1);

  /* test 1 */
  char * msg = NULL; /* if NULL, XDR allocated memory for the decoded string. */
  echo_hello(client, NULL, &msg);
  printf("msg = %s\n", msg);

  /* test 2 */
  char * msg2 = "goodbye world!";
  echo_goodbye(client, &msg2, NULL);

  /* test 3 */
  struct two_int_t xy; 
  xy.x = 10; xy.y = 1;
  int sum = 0;
  echo_sum(client, &xy, &sum);
  printf("x=%d, y=%d, sum=%d\n", xy.x, xy.y, sum);

  /* test 4 */
  struct person_t person; 
  person.firstname = "chuck";
  person.lastname = "norris";
  bool test = false;
  echo_setperson(client, &person, &test);
  printf("boolean test = %d\n", test);

  /* test 5 */
  xarray array;
  array.size = 10;
  array.array = (int*)malloc(array.size*sizeof(int));
  for(int i = 0 ; i < array.size; i++) array.array[i] = i;
  echo_testarray(client, &array, &array);
  printf("array[%d] = (", array.size);
  for(int i = 0 ; i < array.size; i++)
    printf("%d ", array.array[i]);
  printf("\b)\n");
  

  rmi_client_destroy(client);

  return EXIT_SUCCESS;
}
